package com.sc.adminspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.adminspringboot.pojo.PostImages;
import com.sc.adminspringboot.service.IPostImagesService;
import com.sc.adminspringboot.utils.ResponseMsg;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 帖子图片关联表 前端控制器
 * </p>
 *
 * @author 万超凡
 * @since 2025-09-20 10:36:46
 */
@RestController
@RequestMapping("/postImages")
public class PostImagesController {
    //TODO 帖子图片的相关接口

    @Autowired
    IPostImagesService postImagesService;

    /**
     * 分页查询帖子图片
     * @param page
     * @param limit
     * @param req
     * @return
     */
    @RequestMapping("page")
    ResponseMsg pageQuery(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "20") Integer limit,
                          HttpServletRequest req) {



        IPage<PostImages> pageWrapper = new Page<>(page, limit);
        QueryWrapper<PostImages> wrapper = new QueryWrapper<>();
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"查询成功",postImagesService.page(pageWrapper, wrapper));
    }


    /**
     * 根据帖子ID查询图片列表
     * @param postId 帖子ID
     * @param req
     * @return
     */
    @RequestMapping("getByPostId")
    ResponseMsg getImagesByPostId(@RequestParam Integer postId, HttpServletRequest req) {



        if (postId == null) {
            return ResponseMsg.getInstance(ResponseMsg.PARAM_ERROR,"帖子ID不能为空");
        }

        try {
            List<PostImages> images = postImagesService.getImagesByPostId(postId);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"查询成功",images);
        } catch (Exception e) {
            System.out.println("查询帖子图片失败: " + e.getMessage());
            return ResponseMsg.getInstance(ResponseMsg.SERVER_ERROR,"查询失败");
        }
    }

    @RequestMapping("delete")
    ResponseMsg delete(@RequestParam Integer id) {
        if (id == null) {
            return ResponseMsg.getInstance(ResponseMsg.PARAM_ERROR,"图片对应的post id不能为空");
        }

        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"删除成功",postImagesService.removeById(id));
    }

}


